package com.children.growth.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.children.growth.common.Result;
import com.children.growth.entity.Drawing;
import com.children.growth.service.DrawingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 绘画控制器
 * 
 * @author children
 * @since 1.0.0
 */
 @RestController
 @RequestMapping("/drawing")
public class DrawingController {

    @Autowired
    private DrawingService drawingService;

    /**
     * 创建绘画记录
     */
    @PostMapping("/create")
    public Result<Boolean> createDrawing(@RequestParam("imageFile") MultipartFile imageFile,
                                        @RequestParam("title") String title,
                                        @RequestParam("description") String description,
                                        @RequestParam("userId") Long userId) {
        try {
            System.out.println("接收到创建绘画请求:");
            System.out.println("userId: " + userId);
            System.out.println("title: " + title);
            System.out.println("description: " + description);
            System.out.println("imageFile: " + (imageFile != null ? imageFile.getOriginalFilename() : "null"));
            
            Drawing drawing = new Drawing();
            drawing.setTitle(title);
            drawing.setDescription(description);
            drawing.setUserId(userId);
            drawing.setStatus(1);
            
            boolean success = drawingService.createDrawing(drawing, imageFile);
            return success ? Result.success(true) : Result.error("创建绘画记录失败");
        } catch (Exception e) {
            System.err.println("创建绘画记录异常: " + e.getMessage());
            e.printStackTrace();
            return Result.error("创建绘画记录失败: " + e.getMessage());
        }
    }

    /**
     * 获取绘画列表
     */
    @GetMapping("/list")
    public Result<Page<Drawing>> getDrawingList(@RequestParam("userId") Long userId,
                                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<Drawing> page = drawingService.getDrawingList(userId, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 获取绘画详情
     */
    @GetMapping("/{id}")
    public Result<Drawing> getDrawingById(@PathVariable Long id) {
        Drawing drawing = drawingService.getDrawingById(id);
        return drawing != null ? Result.success(drawing) : Result.error("绘画记录不存在");
    }

    /**
     * 更新绘画记录
     */
    @PutMapping("/update")
    public Result<Boolean> updateDrawing(@RequestBody Drawing drawing) {
        boolean success = drawingService.updateDrawing(drawing);
        return success ? Result.success(true) : Result.error("更新绘画记录失败");
    }

    /**
     * 删除绘画记录
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteDrawing(@PathVariable Long id) {
        boolean success = drawingService.deleteDrawing(id);
        return success ? Result.success(true) : Result.error("删除绘画记录失败");
    }

    /**
     * AI分析画作
     */
    @GetMapping("/analyze/{id}")
    public Result<Map<String, Object>> analyzeDrawing(@PathVariable Long id) {
        Map<String, Object> analysis = drawingService.analyzeDrawing(id);
        return analysis != null ? Result.success(analysis) : Result.error("画作分析失败");
    }

    /**
     * 获取绘画统计
     */
    @GetMapping("/statistics/{userId}")
    public Result<Map<String, Object>> getDrawingStatistics(@PathVariable Long userId) {
        Map<String, Object> statistics = drawingService.getDrawingStatistics(userId);
        return Result.success(statistics);
    }
} 